<script>
export default {
  name: "Register",
  data(){
    return {
      //表单数据对象:model
      registerData:{
        userAccount:null,//属性v-model
        userPassword:null,
        userName:null
      },
      //验证
      registerRules:{
        userAccount: [
          { required: true, message: '请输入账号', trigger: 'blur' },
          { min: 1, max: 10, message: '长度在 1 到 10 个字符', trigger: 'blur' }
        ],
        userPassword: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 1, max: 10, message: '长度在 1 到 10 个字符', trigger: 'blur' }
        ],
        userName: [
          { required: true, message: '请输入名字', trigger: 'blur' },
          { min: 1, max: 10, message: '长度在 1 到 10 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods:{
    register(){
      //全局验证
      this.$refs.registerForm.validate((valid) => {
        if (valid) {
          //验证成功，注册
          this.$http({url:"/user/register",method:"POST",data:this.registerData}).then(res=>{
            if(res.data.code==200){
              //注册成功
              this.$message({
                message: res.data.message,
                type: 'success'
              });
              //重置表单
              this.$refs.registerForm.resetFields();
              //跳转路由
              this.$router.push("/")
            }else{
              //注册失败
              this.$message.error(res.data.message);
              //重置表单
              this.$refs.registerForm.resetFields();
            }
          })
        } else {
          this.$message.error('请输入正确的注册信息');
          return false;
        }
      });

    },
    tologin(){
      this.$router.push("/")
    }

  }
}
</script>

<template>
  <div id="login">

    <div id="login_div">
      <h1>注册</h1>
      <!--element-ui表单
         ref="loginForm":给当前表单对象取名loginForm
         model:绑定表单数据对象,form
         v-model:绑定表单中input数据，表单元素input\select。。。对象的属性

         //表单验证
         :rules：表单需要验证，引入验证对象loginRules
         prop="userAccount"：哪一个元素需要验证，引入对象的验证规制userAccount
         绑定数据data
          //验证
      loginRules:{
        userAccount: [
          { required: true, message: '请输入账号', trigger: 'blur' },
          { min: 1, max: 5, message: '长度在 1 到 5 个字符', trigger: 'blur' }
        ],
        userPassword: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 1, max: 5, message: '长度在 1 到 5 个字符', trigger: 'blur' }
        ]
      }
      -->
      <el-form  ref="registerForm" :model="registerData" :rules="registerRules">
        <el-form-item prop="userName">
          <el-input prefix-icon="el-icon-user" v-model="registerData.userName"></el-input>
        </el-form-item>
        <el-form-item prop="userAccount">
          <el-input prefix-icon="el-icon-s-custom" v-model="registerData.userAccount"></el-input>
        </el-form-item>
        <el-form-item prop="userPassword">
          <el-input prefix-icon="el-icon-lock" v-model="registerData.userPassword"></el-input>
        </el-form-item>
        <el-button type="primary" @click="register">注册</el-button>
        <el-button type="primary" @click="tologin">去登陆</el-button>
      </el-form>
    </div>

  </div>
</template>

<style scoped>
#login{
  height: 100%;
  background-image: url("../assets/login.jpg");
  background-repeat: no-repeat;
  background-size: 100%;
}
#login_div{
  width: 300px;
  position: absolute;
  top: 50%;
  left: 70%;
  transform: translate(-50%,-50%);

}
</style>
